package com.dragon.modules.yuegou.client.ali.service.impl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayMarketingCardDeleteModel;
import com.alipay.api.domain.AlipayMarketingCardOpenModel;
import com.alipay.api.domain.AlipayMarketingCardQueryModel;
import com.alipay.api.domain.AlipayMarketingCardUpdateModel;
import com.alipay.api.request.AlipayMarketingCardDeleteRequest;
import com.alipay.api.request.AlipayMarketingCardOpenRequest;
import com.alipay.api.request.AlipayMarketingCardQueryRequest;
import com.alipay.api.request.AlipayMarketingCardUpdateRequest;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.response.AlipayMarketingCardDeleteResponse;
import com.alipay.api.response.AlipayMarketingCardOpenResponse;
import com.alipay.api.response.AlipayMarketingCardQueryResponse;
import com.alipay.api.response.AlipayMarketingCardUpdateResponse;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.dragon.modules.yuegou.client.ali.request.MarketingCardDelRequest;
import com.dragon.modules.yuegou.client.ali.request.MarketingCardOpenRequest;
import com.dragon.modules.yuegou.client.ali.request.MarketingCardQueryRequest;
import com.dragon.modules.yuegou.client.ali.request.MarketingCardUpdRequest;
import com.dragon.modules.yuegou.client.ali.request.OauthTokenRequest;
import com.dragon.modules.yuegou.client.ali.response.AliBaseResponse;
import com.dragon.modules.yuegou.client.ali.response.MarketingCardOpenResponse;
import com.dragon.modules.yuegou.client.ali.response.MarketingCardQueryResponse;
import com.dragon.modules.yuegou.client.ali.response.OauthTokenResponse;
import com.dragon.modules.yuegou.client.ali.service.MemCardService;
import com.dragon.modules.yuegou.properties.AlipayXcxConfig;
import com.dragon.modules.yuegou.properties.IsvAlipayProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class MemCardServiceImpl implements MemCardService {

    @Autowired
    private IsvAlipayProperties isvAlipayProperties;

	@Override
	public OauthTokenResponse oauthToken(OauthTokenRequest request) {
		OauthTokenResponse response = new OauthTokenResponse();
        AlipayClient alipayClient =  new DefaultAlipayClient(isvAlipayProperties.getServerUrl(),isvAlipayProperties.getAppId(),
        		isvAlipayProperties.getPrivateKey(),isvAlipayProperties.getFormat(),isvAlipayProperties.getCharset(),isvAlipayProperties.getAlipayPublicKey(),isvAlipayProperties.getSignType());
            AlipaySystemOauthTokenRequest setRequest = new AlipaySystemOauthTokenRequest();
            setRequest.setGrantType("authorization_code");
            setRequest.putOtherTextParam(AlipayXcxConfig.APP_AUTH_TOKEN_STRING, request.getAppAuthToken());
            setRequest.setCode(request.getCode());
            try {
                AlipaySystemOauthTokenResponse setResponse = alipayClient.execute(setRequest);
                log.debug(JSON.toJSONString(setResponse));
            	BeanUtils.copyProperties(setResponse,response);
                if(setResponse.isSuccess()){
                    log.debug("调用成功");
                	response.setUserId(setResponse.getUserId());
                	response.setAccessToken(setResponse.getAccessToken());
                } else {
                    log.debug("调用失败");
                }
            } catch (AlipayApiException e) {
                e.printStackTrace();
            }       
		return response;
	}


	@Override
	public MarketingCardOpenResponse marketingCardOpen(MarketingCardOpenRequest request) {
		MarketingCardOpenResponse response = new MarketingCardOpenResponse();
        AlipayClient alipayClient =  new DefaultAlipayClient(isvAlipayProperties.getServerUrl(),isvAlipayProperties.getAppId(),
        		isvAlipayProperties.getPrivateKey(),isvAlipayProperties.getFormat(),isvAlipayProperties.getCharset(),isvAlipayProperties.getAlipayPublicKey(),isvAlipayProperties.getSignType());
         AlipayMarketingCardOpenRequest setRequest = new AlipayMarketingCardOpenRequest();
         setRequest.putOtherTextParam(AlipayXcxConfig.APP_AUTH_TOKEN_STRING, request.getAppAuthToken());
        AlipayMarketingCardOpenModel model = new AlipayMarketingCardOpenModel();
        model.setOutSerialNo(request.getOutSerialNo());
        model.setCardTemplateId(request.getCardTemplateId());
        model.setCardExtInfo(request.getCardExtInfo());
        model.setCardUserInfo(request.getCardUserInfo());
        model.setMemberExtInfo(request.getMemberExtInfo());
        model.setOpenCardChannel(request.getOpenCardChannel());
        model.setOpenCardChannelId(request.getOpenCardChannelId());
        setRequest.setBizModel(model);
        try {
        	AlipayMarketingCardOpenResponse setResponse = alipayClient.execute(setRequest,request.getAuthToken());
            log.debug(JSON.toJSONString(setResponse));
        	BeanUtils.copyProperties(setResponse,response);
            if(setResponse.isSuccess()){
                log.debug("调用成功");
            	response.setBizCardNo(setResponse.getCardInfo().getBizCardNo());
            	response.setTemplateId(setResponse.getCardInfo().getTemplateId());
            } else {
                log.debug("调用失败");
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }  
        return response;
	}

	@Override
	public AliBaseResponse marketingCardUpd(MarketingCardUpdRequest request) {
		AliBaseResponse response = new AliBaseResponse();
	    AlipayClient alipayClient =  new DefaultAlipayClient(isvAlipayProperties.getServerUrl(),isvAlipayProperties.getAppId(),
	        		isvAlipayProperties.getPrivateKey(),isvAlipayProperties.getFormat(),isvAlipayProperties.getCharset(),isvAlipayProperties.getAlipayPublicKey(),isvAlipayProperties.getSignType());
        AlipayMarketingCardUpdateRequest   setRequest = new AlipayMarketingCardUpdateRequest();
        setRequest.putOtherTextParam(AlipayXcxConfig.APP_AUTH_TOKEN_STRING, request.getAppAuthToken());
        AlipayMarketingCardUpdateModel model = new AlipayMarketingCardUpdateModel();
        model.setTargetCardNo(request.getTargetCardNo());
        model.setOccurTime(request.getOccurTime());
        model.setTargetCardNoType(request.getTargetCardNoType());
        model.setCardInfo(request.getMerchantCard());
        setRequest.setBizModel(model);
        try {
        	AlipayMarketingCardUpdateResponse  setResponse = alipayClient.execute(setRequest);
            log.debug(JSON.toJSONString(setResponse));
        	BeanUtils.copyProperties(setResponse,response);
            if(setResponse.isSuccess()){
                log.debug("调用成功");
            } else {
                log.debug("调用失败");
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        } 
        return response;
	}
	
	@Override
	public MarketingCardQueryResponse marketingCardQuery(MarketingCardQueryRequest request) {
		MarketingCardQueryResponse response = new MarketingCardQueryResponse();
	       AlipayClient alipayClient =  new DefaultAlipayClient(isvAlipayProperties.getServerUrl(),isvAlipayProperties.getAppId(),
	        		isvAlipayProperties.getPrivateKey(),isvAlipayProperties.getFormat(),isvAlipayProperties.getCharset(),isvAlipayProperties.getAlipayPublicKey(),isvAlipayProperties.getSignType());
        AlipayMarketingCardQueryRequest  setRequest = new AlipayMarketingCardQueryRequest();
        setRequest.putOtherTextParam(AlipayXcxConfig.APP_AUTH_TOKEN_STRING, request.getAppAuthToken());
        AlipayMarketingCardQueryModel model = new AlipayMarketingCardQueryModel();
        model.setTargetCardNo(request.getTargetCardNo());
        model.setTargetCardNoType(request.getTargetCardNoType());
        setRequest.setBizModel(model);
        try {
        	AlipayMarketingCardQueryResponse  setResponse = alipayClient.execute(setRequest);
            log.debug(JSON.toJSONString(setResponse));
        	BeanUtils.copyProperties(setResponse,response);
            if(response.isSuccess()){
                log.debug("调用成功");
            } else {
            	log.debug("调用失败");
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return response;
	}

	@Override
	public AliBaseResponse marketingCardDel(MarketingCardDelRequest request) {
		AliBaseResponse response = new AliBaseResponse();
	       AlipayClient alipayClient =  new DefaultAlipayClient(isvAlipayProperties.getServerUrl(),isvAlipayProperties.getAppId(),
	        		isvAlipayProperties.getPrivateKey(),isvAlipayProperties.getFormat(),isvAlipayProperties.getCharset(),isvAlipayProperties.getAlipayPublicKey(),isvAlipayProperties.getSignType());
        AlipayMarketingCardDeleteRequest setRequest = new AlipayMarketingCardDeleteRequest();
        setRequest.putOtherTextParam(AlipayXcxConfig.APP_AUTH_TOKEN_STRING, request.getAppAuthToken());
        AlipayMarketingCardDeleteModel model = new AlipayMarketingCardDeleteModel();
        setRequest.setBizModel(model);
        try {
        	AlipayMarketingCardDeleteResponse  setResponse = alipayClient.execute(setRequest,request.getAppAuthToken());
            log.debug(JSON.toJSONString(setResponse));
        	BeanUtils.copyProperties(setResponse,response);
            if(setResponse.isSuccess()){
                log.debug("调用成功");
            } else {
                log.debug("调用失败");
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        } 
        return response;
	}



}
